*******************************************************************************
*Table B.3: Effects on value received using alternative specifications
*This table reports alternative specifications for Panel B in Table A.5 by reporting models with March data only, pooled data with a linear trend, and pooled data without baseline lag
*******************************************************************************

use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

*******************************************************************************
* Keep if surveyed or classified as ghost
keep if ss_code == "SS01" | ghost_final == 1


*******************************************************************************
svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)


count
scalar obs = 3960 - ghosts

local tflist ""
local tflist_noBL ""

*Analysis
loc ration "rice wheat sugar salt kero"

foreach rat of local ration{
gen bl_var = b_overcharge_`rat'_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


qui svy: mean b_overcharge_`rat'_mar17 if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo mar_`rat'BL: xi: reg b_overcharge_`rat'_mar17 treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs


tempfile mar_`rat'BL
parmest, label saving("`mar_`rat'BL'")

local tflist "`tflist' `mar_`rat'BL'"



eststo mar_`rat': xi: reg b_overcharge_`rat'_mar17 treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs

tempfile mar_`rat'
parmest, label saving("`mar_`rat''")
local tflist_noBL "`tflist_noBL' `mar_`rat''"

drop bl_var*
}

*Total overcharge
gen bl_var = b_overcharge_total_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


qui svy: mean b_overcharge_total_mar17 if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo mar_totalBL: xi: reg b_overcharge_total_mar17 treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs

eststo mar_total: xi: reg b_overcharge_total_mar17 treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs

drop bl_var*

*Write table
preserve
dsconcat `tflist',subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local estname: word `i' of `tflist'
local rat: word `i' of `ration'

estimates restore mar_`rat'BL

mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue) 

}


restore

loc tabname "${OutputDir}/TableB_3.tex" 
cd "${adoDir}"
loc nc 6				
				
MultiPartTabStart, ///
			ncol(`nc') tabname(`tabname') ///
			colnames("Total" "Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize")			
				
				
MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel A: March only")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("*totalBL *riceBL *wheatBL *sugarBL *saltBL *keroBL") ///
				drop(_cons _Istrata* bl_var*) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) )  ///
				starlevels( * 0.10 ** 0.05 *** 0.01) ///
				varlabels(treatment "Treatment" , elist(treatment \addlinespace )  )  ///
				stats(control_mean N,  ///
					labels("Control mean" "Observations") fmt(2 %12.2gc 0 0))
		

preserve

dsconcat `tflist_noBL',subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local estname: word `i' of `tflist_noBL'
local rat: word `i' of `ration'

estimates restore mar_`rat'

mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue) 
}


restore


*******************************************
* Pooled
*******************************************

local tflist ""
local tflist_noBL ""
local tflist_trend ""
local tflist_trend_noBL ""

foreach rat of local ration{
gen bl_var = b_overcharge_`rat'_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


preserve
keep uid b_overcharge_`rat'_jan17 b_overcharge_`rat'_feb17 b_overcharge_`rat'_mar17 bl_var bl_var_mi treatment strata pweight block_code
gen b_overcharge_`rat'3 = b_overcharge_`rat'_mar17
gen b_overcharge_`rat'2 = b_overcharge_`rat'_feb17
gen b_overcharge_`rat'1 = b_overcharge_`rat'_jan17
drop b_overcharge_`rat'_jan17 b_overcharge_`rat'_feb17 b_overcharge_`rat'_mar17
reshape long b_overcharge_`rat', i(uid) j(month)

count
scalar obs_all = 3*(3960 - ghosts)

gen treatmentXmonth=treatment*month

qui svy: mean b_overcharge_`rat' if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

eststo `rat'pool1: xi: reg b_overcharge_`rat' treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

tempfile `rat'pool1
parmest, label saving("``rat'pool1'")
local tflist_noBL "`tflist_noBL' ``rat'pool1'"


qui svy: mean b_overcharge_`rat' if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo `rat'pool2: xi: reg b_overcharge_`rat' treatment i.strata month treatmentXmonth  [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

tempfile `rat'pool2
parmest, label saving("``rat'pool2'")
local tflist_trend_noBL "`tflist_trend_noBL' ``rat'pool2'"


eststo `rat'poolBL1: xi: reg b_overcharge_`rat' treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

tempfile `rat'poolBL1
parmest, label saving("``rat'poolBL1'")
local tflist "`tflist' ``rat'poolBL1'"


qui svy: mean b_overcharge_`rat' if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo `rat'poolBL2: xi: reg b_overcharge_`rat' treatment bl_var bl_var_mi i.strata month treatmentXmonth  [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

tempfile `rat'poolBL2
parmest, label saving("``rat'poolBL2'")
local tflist_trend "`tflist_trend' ``rat'poolBL2'"

restore

drop bl_var*
}

*Total overcharge
preserve

gen bl_var = b_overcharge_total_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

keep uid b_overcharge_total_*17 bl_var bl_var_mi treatment strata pweight block_code
gen b_overcharge_total3 = b_overcharge_total_mar17
gen b_overcharge_total2 = b_overcharge_total_feb17
gen b_overcharge_total1 = b_overcharge_total_jan17
drop b_overcharge_total_*17
reshape long b_overcharge_total, i(uid) j(month)

count
scalar obs_all = 3*(3960 - ghosts)

gen treatmentXmonth=treatment*month

qui svy: mean b_overcharge_total if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

eststo totalpool1: xi: reg b_overcharge_total treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all

qui svy: mean b_overcharge_total if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo totalpool2: xi: reg b_overcharge_total treatment i.strata month treatmentXmonth  [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all


eststo totalpoolBL1: xi: reg b_overcharge_total treatment bl_var bl_var_mi i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all


qui svy: mean b_overcharge_total if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo totalpoolBL2: xi: reg b_overcharge_total treatment bl_var bl_var_mi i.strata month treatmentXmonth  [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/obs_all


drop bl_var*
restore


*Write tables
preserve
dsconcat `tflist', subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local rat: word `i' of `ration'
estimates restore `rat'poolBL1

mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue) 


}

restore


preserve
dsconcat `tflist_noBL', subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local rat: word `i' of `ration'
estimates restore `rat'pool1

mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue) 


}
restore


preserve
dsconcat `tflist_trend', subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local rat: word `i' of `ration'
estimates restore `rat'poolBL2


mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue) 
}


restore


MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel B: Pooled data with linear trend")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("totalpoolBL2 ricepoolBL2 wheatpoolBL2 sugarpoolBL2 saltpoolBL2 keropoolBL2") ///
				drop(_cons _Istrata* bl_var*) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) )  ///
				starlevels( * 0.10 ** 0.05 *** 0.01) ///
				varlabels(treatment "Treatment"  month "Month" treatmentXmonth "Treatment X Month" , ///
				elist(treatment \addlinespace  month \addlinespace treatmentXmonth \addlinespace)  ) ///
				stats(control_mean N, labels("Control mean" "Observations") fmt(2 %12.2gc 0 0))

MultiPartTabPanelStart, ///
			ncol(`nc') tabname(`tabname') ///
			panelstring("Panel C: Pooled data with no baseline lag")
			
MultiPartTabPanelEnd, ///
				ncol(`nc') tabname(`tabname') ///
				models("totalpool1 ricepool1 wheatpool1 sugarpool1 saltpool1 keropool1") ///
				drop(_cons _Istrata*) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) )  ///
				starlevels( * 0.10 ** 0.05 *** 0.01) ///
				varlabels(treatment "Treatment" elist(treatment \addlinespace )  ) ///
				stats(control_mean N, labels("Control mean" "Observations") fmt(2 %12.2gc 0 0))

MultiPartTabEnd, ///
				ncol(`nc') tabname(`tabname')

